package com.tn.exam.interceptor.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.tn.exam.util.taoyf.web.RequestUtil;

@Component
public class StrutsLogger implements Interceptor {
	
	private static Logger log = LoggerFactory.getLogger(StrutsLogger.class);
	private static final String MESSAGE_COME = "访问过来:";
	private static final String MESSAGE_RETURN = "Action处理完，返回数据:";

	public void destroy() {
		
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		log.info(MESSAGE_COME) ;
		logMessage(invocation);
		String result = invocation.invoke();
		log.info(MESSAGE_RETURN);
	    return result;
	}
	
    private void logMessage(ActionInvocation invocation) {
    	logUrl(invocation);
        HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
        RequestUtil.logRequestParameter(request);
    }
    
    private void logUrl(ActionInvocation invocation){
        StringBuilder message = new StringBuilder();
        String namespace = invocation.getProxy().getNamespace();

        if ((namespace != null) && (namespace.trim().length() > 0)) {
            message.append(namespace).append("/");
        }

        message.append(invocation.getProxy().getActionName());
        log.info(message.toString());
    }


}
